package com.funny.mid;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * @author fanxiao  2021/8/20
 * @since 1.0.0
 */
public class Resources {

	public static Reader getResourceAsReader(String resource) throws IOException {
		return new InputStreamReader(getResourceAsStream(resource));
	}

	private static InputStream getResourceAsStream(String resource) throws IOException {
		ClassLoader[] classLoaders = getClassLoaders();
		for (ClassLoader classLoader : classLoaders) {
			InputStream inputStream = classLoader.getResourceAsStream(resource);
			if (null != inputStream) {
				return inputStream;
			}
		}
		throw new IOException("Could not find resource " + resource);
	}

	private static ClassLoader[] getClassLoaders() {
		return new ClassLoader[]{
				ClassLoader.getSystemClassLoader(),
				Thread.currentThread().getContextClassLoader()};
	}

}
